package http

import (
	"context"
	"net/http"
	"ruoyi-go-echo-ddd/internal/infrastructure/http/handler"
	cus "ruoyi-go-echo-ddd/internal/infrastructure/http/middleware"
	"ruoyi-go-echo-ddd/internal/infrastructure/http/request"
	"ruoyi-go-echo-ddd/internal/infrastructure/http/router"

	"github.com/go-playground/validator/v10"
	"github.com/google/wire"
	"github.com/labstack/echo/v4"
	"github.com/labstack/echo/v4/middleware"
)

// ProviderSet wire 注入使用
var ProviderSet = wire.NewSet(
	NewEchoRouter,
)

// Router 定义 Router 接口
type Router interface {
	Start(address string) error
	Shutdown(ctx context.Context) error
}

//goland:noinspection GoUnhandledErrorResult
func NewEchoRouter(
	sysMenuResources *handler.SysMenuResourcesHandler,
	sysDept *handler.SysDeptHandler,
	upload *handler.UploadHandler,
	sysRole *handler.SysRoleHandler,
	sysMenu *handler.SysMenuHandler,
	sysPost *handler.SysPostHandler,
	validatorCode *handler.ValidatorCodeHandler,
	minio *handler.MinioHandler,
) (*echo.Echo, error) {

	e := echo.New()
	// 设置请求体大小限制为 500MB
	e.Use(middleware.BodyLimit("500M"))
	e.Use(cus.RequestIdLoggerMiddleware())
	e.Validator = &request.CustomValidator{Validator: validator.New()}
	e.Use(cus.ValidatorMiddleware())

	// 设置echo服务请求前缀
	group := e.Group("")
	{
		router.NewSysMenuResourcesRouter(group, sysMenuResources)
		router.NewSysDeptRouter(group, sysDept)
		router.NewUploadRouter(group, upload)
		router.NewSysRoleRouter(group, sysRole)
		router.NewSysMenuRouter(group, sysMenu)
		router.NewSysPostRouter(group, sysPost)
		router.NewValidatorCodeRouter(group, validatorCode)
		router.NewMinioRouter(group, minio)
	}

	// 设置静态资源目录
	//e.Static("/static", "./resources/static")

	e.RouteNotFound("/*", func(c echo.Context) error {
		return c.JSON(http.StatusNotFound, map[string]any{
			"msg": "404 - 页面未找到",
		})
	})

	return e, nil
}
